<?php

require_once realpath (dirname (__FILE__) . '//..//pages.php');
require_once CLASS_USER;
require_once INC_DEFINES;
require_once INC_FUNCTIONS;
require_once INC_MESSAGES;

checkSession ();
/* Getting data */
if (isset ($_SESSION['data'])) {
    $post = true;
    $data = array_merge_recursive ($_GET, $_SESSION['data']);
} else {
    $post = false;
    $data = $_GET;
}
/* Getting ID */
if (isset ($data['ID'])) {
    $ID = $data['ID'] ? $data['ID'] : null;
} else {
    $ID = null;
}

if (isset ($data['order_by'])) {
    $order_by = $data['order_by'];
    if (isset ($data['order'])) {
        if ($data['order'] == 'desc') {
            $order_by .= ' DESC';
        } else {
            $order_by .= ' ASC';
        }
    } else {
        $order_by .= ' ASC';
    }
} else {
    $order_by = null;
}
/* Checking request */
if (isset ($data['request'])) {
    $request = $data['request'] ? $data['request'] : null;
} else {
    $msg = MSG_INVALID_REQUEST;
    require_once VW_NOTIFY;
    exit ();
}
/* Checking table */
if (isset ($data['table'])) {
    $table = $data['table'] ? $data['table'] : null;
} else {
    require_once VW_TABLES;
    exit ();
}

if (!authorize ($request, $table)) {
    $msg = MSG_NOT_AUTHORIZED;
    require_once VW_NOTIFY;
    exit ();
}

if (isset ($data['table'])) {
    $table = $data['table'];
    switch ($request) {
        case 'delete' :
            if ($ID) {
                require_once MDL_REQ_DELETE;
            } else {
                require_once MDL_REQ_PG_LIST;
                /* $result is used by VW_LIST */
                $result = show ($table, $order_by);
                require_once VW_LIST;
            }
            break;
        case 'edit' :
            if ($ID) {
                if ($post) {
                    require_once MDL_REQ_EDIT;
                } else {
                    require_once MDL_REQ_PG_EDIT;
                }
            } else {
                require_once MDL_REQ_PG_LIST;
                /* $result is used by VW_LIST */
                $result = show ($table, $order_by);
            }
            break;
        case 'insert' :
            if (isset ($data['show'])) {
                require_once MDL_REQ_PG_LIST;
                $result = show ($table, $order_by);
            } else {
                if ($post) {
                    require_once MDL_REQ_INSERT;
                } else {
                    require_once MDL_REQ_PG_INSERT;
                }
            }
            break;
        case 'view' :
            if (isset ($data['values'])) {
                $values = explode (',', $data['values']);
            } else {
                $values = null;
            }
            if (count ($values) == 1) {
                $view = true;
                $data['ID'] = $values[0];
                require_once MDL_REQ_PG_EDIT;
            } else {
                require_once MDL_REQ_PG_LIST;
                $result = show ($table, $order_by, $values);
            }
            break;
    }
}
switch ($table) {
    case 'classe' :
        $html = classe ($data);
        break;
    case 'distretto' :
        $html = distretto ($data);
        break;
    case 'docente' :
        $html = docente ($data);
        break;
    case 'indirizzo' :
        $html = indirizzo ($data);
        break;
    case 'intervento' :
        $html = intervento ($data);
        break;
    case 'materia' :
        $html = materia ($data);
        break;
    case 'ospedale' :
        $html = ospedale ($data);
        break;
    case 'permesso' :
        $html = permesso ($data);
        break;
    case 'reparto' :
        $html = reparto ($data);
        break;
    case 'ricovero' :
        $html = ricovero ($data);
        break;
    case 'scuola':
        $html = scuola ($data);
        break;
    case 'studente' :
        $html = studente ($data);
        break;
    case 'supervisiona':
        $html = supervisiona ($data);
        break;
    case 'tipo_intervento' :
        $html = tipoIntervento ($data);
        break;
    case 'tipo_ricovero' :
        $html = tipoRicovero ($data);
        break;
    case 'user' :
        $html = user ($data);
        break;
}
switch ($request) {
    case 'delete' :
        if (isset ($data['ID'])) {
            header ('location: ' . HTTP_CTRL_REQUEST . "?request=$request&table=$table");
        } else {
            require_once VW_LIST;
        }
        break;
    case 'edit' :
        if (isset ($data['ID'])) {
            if ($post) {
                header ('location: ' . HTTP_CTRL_REQUEST . "?request=$request&table=$table");
            } else {
                require_once VW_INSERT;
            }
        } else {
            require_once VW_LIST;
        }
        break;
    case 'insert' :
        if (isset ($data['show'])) {
            require_once VW_LIST;
        } else {
            if ($post) {
                header ('location: ' . HTTP_CTRL_REQUEST . "?request=view&table=$table");
            } else {
                require_once VW_INSERT;
            }
        }
        break;
    case 'tables' :
        require_once VW_TABLES;
        exit ();
        break;
    case 'view' :
        if (count ($values) == 1) {
            $disable_input = true;
            require_once VW_INSERT;
        } else {
            require_once VW_LIST;
        }
        break;
    default :
        $msg = 'Invalid request!';
        require VW_NOTIFY;
        exit ();
        break;
}

if ($post) {
    unset ($_SESSION['data']);
}
?>
